<?php
/**
 * 自动加载类
 * 
 * Copyright (c) 2014~2019 https://desion.net All rights reserved.
 * Author: 阿林 QQ:120867629 <linhong00@gmail.com>
 */
declare (strict_types = 1);
namespace dsphp;

class Loader
{
    // 路径地图
    private static $filesMap = [];


    // 注册自动加载机制
    public static function register()
    {
        spl_autoload_register('dsphp\\Loader::autoload', true, true);
    }

    // 自动加载
    public static function autoload($class)
    {
        // 组合文件路径
        $filePath = self::getFilePath($class);

        self::includeFile($filePath);
    }

    // 组合路径
    public static function getFilePath($class)
    {
        if (isset(self::$filesMap[$class])) {
            return self::$filesMap[$class];
        }

        $fileType = (substr($class, 0 , 5) == 'dsphp') ? '' : DSPHP_WEB . DIRECTORY_SEPARATOR;
        $filePath = DSPHP_PATH .$fileType. str_replace('\\', '/', $class) . DSPHP_EXT;
        
        self::$filesMap[$class] = $filePath;
 
        return $filePath;
    }

    // 加载文件
    public static function includeFile($file)
    {
        include $file;
    }
}
